package net.comic.controllers

import net.comic.entities.Comic
import net.comic.entities.User
import net.comic.services.ComicService
import net.comic.services.CommentService


class ComicController {
    def comicService
    def rateService
    def commentService
    
    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", delete: "POST", update: "POST"]

    def list = {
    	session['userLogged'] = User.findByAlias("admin")
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        
        if (params.finderInput != null) {
        	[comicInstanceList: params.finderInput, comicInstanceTotal: params.finderInput.size()]
        }else{
        	[comicInstanceList: Comic.list(params), comicInstanceTotal: Comic.count()]
        }
    }

    def create = {
        def comicInstance = new Comic()
        comicInstance.properties = params
        return [comicInstance: comicInstance]
    }

    def save = {
    	def comicInstance = new Comic(params)
    	if(comicInstance.validate()) {
    		def responseSrvCtl = comicService.addComic(request.getFile('file'), comicInstance)
    		if(responseSrvCtl.response == "zzzz"){
				flash.message = "comic.created"
	            flash.args = [comicInstance.id]
	            flash.defaultMessage = "Comic ${comicInstance.id} created"
	            redirect(action: "show", id: comicInstance.id)
    			return
	    	}
    	}
    	render(view: "create", model: [comicInstance: comicInstance])    	
    }

    def show = {
        def comicInstance = Comic.get(params.id)
        if (!comicInstance) {
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
        	session['myRate']  = userService.getMyRate(comicInstance.ratings, session['userLogged'])
        	log.debug "${comicInstance?.rate}"
            return [comicInstance: comicInstance]
        }
    }

    def edit = {
        def comicInstance = Comic.get(params.id)
        if (!comicInstance) {
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [comicInstance: comicInstance]
        }
    }

    def enable = {
    	params.disabled = false
    	comicService.update(params)
    }
    
    def disable = {
    	params.disabled = true
    	comicService.update(params)
    }
    
    def update = {
    	println "dins del update " + params
        def result = comicService.update(params)

        if(result != null) {
        	if(result.hasErrors()){
        		render(view: "edit", model: [comicInstance: result])
        	}else{
        		flash.message = "comic.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} updated"
                redirect(action: "show", id: result.id)
        	}
        }else{
            flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
    	
        def comicInstance = Comic.get(params.id)
        if (comicService.delete(comicInstance)) {
        	if(comicInstance.hasErrors()) {
        		flash.message = "comic.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
        	} else {
        	    flash.message = "comic.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Comic ${params.id} deleted"
                redirect(action: "list")
        	}
        } else {
        	flash.message = "comic.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Comic not found with id ${params.id}"
            redirect(action: "list")
        }
    }
    
    
    def rate = {
    	rateService.rateComic(params.id, params.userRateValue, session['userLogged'])
    	redirect(action: "show", id: params.id)
    }
    
    def favorite = {
    	params.id = "1"
    	def user = User.get(1)
    	def comic = Comic.get(1)
    	user.addToFavoritesComics(comic)
    	redirect(action: "show", id: params.id)
    }
    
    def comment = {
    	println "eeeeee " + params
    	
    	def result = commentService.commentComic(params, params.id, session['userLogged'])
        println result
        
        if(result.result) {
        	flash.message = "SE HA GUARADADO CON EXITO"
        	redirect(controller:"comic", action: "show", id: result.instance.id, params:[kaka: true])
        } else {
        	redirect(controller:"comic", action: "show", id: result.instance.id, params:[kaka: false])
        }
        
        
        
    	//redirect(controller:"comment", action: "create", id: params.id)
    }
}
